<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="赛事名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" @click="handleSubject()">
        项目列表
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="conList"
      element-loading-text="Loading"
      border
      fit
      style="width: 100%; margin-top:10px;"
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="150" prop="id">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="赛事名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.subject.matchName }}
        </template>
      </el-table-column>
      <el-table-column label="组赛单位" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.subject.matchUnit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="赛制" width="100" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.subject.matchRules === 0">
            个人赛
          </div>
          <div v-else>
            团队赛
          </div>
        </template>
      </el-table-column>
      <el-table-column label="获奖名次" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.prizeAward === 0">
            特等奖
          </div>
          <div v-if="scope.row.prizeAward === 1">
            一等奖
          </div>
          <div v-if="scope.row.prizeAward === 2">
            二等奖
          </div>
          <div v-if="scope.row.prizeAward === 3">
            三等奖
          </div>
          <div v-if="scope.row.prizeAward === 4">
            优秀奖
          </div>
          <div v-if="scope.row.prizeAward === 5">
            无
          </div>
        </template>
      </el-table-column>
      <el-table-column label="级别" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.level === 0">
            国家级
          </div>
          <div v-if="scope.row.level === 1">
            区级
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="150" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.conStatus === 0">
            未审核
          </div>
          <div v-if="scope.row.conStatus === 1">
            已结题
          </div>
          <div v-if="scope.row.conStatus === 2">
            不通过
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleView(scope.row.id)">
            详情
          </el-button>
          <el-button v-if="scope.row.conStatus !== 1" type="primary" size="mini" @click="handleUpdate(scope.row.subId)">
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />

    <el-dialog title="项目列表" :visible.sync="subjectVisible" width="80%">
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        style="width: 100%; margin-top:10px;"
        highlight-current-row
      >
        <el-table-column align="center" label="序号" width="100" prop="id">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="赛事名称" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.matchName }}
          </template>
        </el-table-column>
        <el-table-column label="组赛单位" width="100" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.matchUnit }}</span>
          </template>
        </el-table-column>
        <el-table-column label="赛制" width="100" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.matchRules === 0">
              个人赛
            </div>
            <div v-else>
              团队赛
            </div>
          </template>
        </el-table-column>
        <el-table-column label="项目负责人" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.subLeader }}
          </template>
        </el-table-column>
        <el-table-column label="联系电话" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.telephone }}
          </template>
        </el-table-column>
        <el-table-column label="电子邮箱" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.email }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleCreate(scope.row.subId)">
              结题申请
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :model="temp" :rules="formRules" label-position="left" label-width="70px" style="width: 500px; margin-left: 50px;">
        <el-form-item label="获奖级别" prop="level" label-width="120px">
          <el-radio-group v-model="temp.level">
            <el-radio label="0">国家级</el-radio>
            <el-radio label="1">区级</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="获奖名次" prop="prizeAward" label-width="120px">
          <el-radio-group v-model="temp.prizeAward">
            <el-radio label="0">特等奖</el-radio>
            <el-radio label="1">一等奖</el-radio>
            <el-radio label="2">二等奖</el-radio>
            <el-radio label="3">三等奖</el-radio>
            <el-radio label="4">优秀奖</el-radio>
            <el-radio label="5">无</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资金预算" label-width="120px" :model="free">
          <div class="" style="width:100%;display:flex;justify-content: space-between;">
            <div style="width:20%">
              <table width="100%">
                <table width="100%">
                  <tr><td class="left_title bottom_border">注册费</td></tr>
                  <tr><td class="left_title bottom_border">差旅费</td></tr>
                  <tr><td class="left_title bottom_border">培训费</td></tr>
                  <tr><td class="left_title bottom_border">评审费</td></tr>
                  <tr><td class="left_title bottom_border">领队费</td></tr>
                  <tr><td class="left_title bottom_border">组织费</td></tr>
                  <tr><td class="left_title bottom_border">指导费</td></tr>
                  <tr><td class="left_title bottom_border">材料费</td></tr>
                  <tr><td class="left_title bottom_border">教师奖金</td></tr>
                  <tr><td class="left_title bottom_border">合计费用</td></tr>
                </table>
              </table>
            </div>
            <div>
              <table width="100%">
                <tr><td class="left_title bottom_border"><input v-model="free.registerFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.businessFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.trainFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.reviewFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.leaderFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.organizeFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.guideFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.materialFree" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.teacherBonus" style="width:300px"></td></tr>
                <tr><td class="left_title bottom_border"><input v-model="free.sumFree" style="width:300px"></td></tr>
              </table>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="结题申请书" prop="file" label-width="120px">
          <el-upload
            ref="upload"
            class="upload-demo"
            :data="temp"
            :multiple="false"
            :headers="myHeaders"
            :on-success="handleFileSuccess"
            action="http://localhost:8088/conclusion/leader/add"
            :auto-upload="false"
          >
            <i class="el-icon-upload" />
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="viewVisible">
      <div>
        意见：
        <a>
          {{ temp.opinion }}
        </a>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewVisible = false">
          取消
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { forConclusion } from '@/api/subject'
import { leaderList, getOne } from '@/api/conclusion'
import Pagination from '@/components/Pagination'
import { getToken } from '@/utils/auth'
import Vue from 'vue'

export default {
  components: { Pagination },
  data() {
    return {
      myHeaders: { Authorization: getToken() },
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        title: undefined,
        leaderId: this.$store.getters.name
      },
      list: null,
      conList: null,
      total: 0,
      listLoading: true,
      textMap: {
        update: '修改',
        create: '项目立项'
      },
      dialogStatus: '',
      dialogFormVisible: false,
      viewVisible: false,
      subjectVisible: false,
      temp: {
        leaderId: this.$store.getters.name
      },
      free: {
        registerFree: '',
        businessFree: '',
        trainFree: '',
        reviewFree: '',
        leaderFree: '',
        organizeFree: '',
        guideFree: '',
        materialFree: '',
        teacherBonus: '',
        sumFree: ''
      },
      uploaddata: {
        subId: ''
      }
    }
  },
  created() {
    this.fetchData()
    this.listLoading = false
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNum = 1
      if (this.listQuery.title === null) {
        this.listQuery.title = undefined
      }
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      forConclusion(this.listQuery).then(response => {
        this.list = response.data.content
        this.total = response.data.totalSize
        this.listLoading = false
      })
      leaderList(this.listQuery).then(response => {
        this.conList = response.data.content
      })
    },
    resetTemp() {
      this.temp = {
        id: '',
        subId: '',
        matchName: '',
        matchUnit: ''
      }
      this.free = {
        registerFree: '',
        businessFree: '',
        trainFree: '',
        reviewFree: '',
        leaderFree: '',
        organizeFree: '',
        guideFree: '',
        materialFree: '',
        teacherBonus: '',
        sumFree: ''
      }
    },
    handleSubject() {
      this.subjectVisible = true
    },
    handleCreate(subId) {
      this.resetTemp()
      this.temp.subId = subId
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          Vue.set(this.temp, 'free', JSON.stringify(this.free))
          this.$refs.upload.submit()
        }
      })
    },
    handleFileSuccess(response, file) {
      if (response.code === 200) {
        setTimeout(() => {
          this.list.push(response.data)
          this.dialogFormVisible = false
          this.$refs.upload.clearFiles()
          this.handleFilter()
          this.$notify({
            title: 'Success',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
        })
      } else {
        this.$refs.upload.clearFiles()
        this.$notify.error({
          message: '添加失败',
          type: 'error'
        })
      }
    },
    handleUpdate(subId) {
      this.resetTemp()
      this.temp.subId = subId
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleView(id) {
      getOne(id).then(response => {
        this.temp = response.data
      })
      this.viewVisible = true
    }
  }
}
</script>
